/* Copyright 2007 Kevin Swope, All Rights Reserved */

/* This file is part of Moonfall. */

/* Moonfall is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License, or */
/* (at your option) any later version. */
  
/* Moonfall is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
/* GNU General Public License for more details. */

/* You should have received a copy of the GNU General Public License */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>. */

// Converts a lua file to a c library file containing lua file as a string

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


//----------------------------------------------------------------------------
// This was copied from mflib.c to avoid recursive include problem.
// This creates file that defines 
//----------------------------------------------------------------------------
char* string_from_file(char *filename) {

  FILE *fp = fopen(filename , "r");
  if ( fp == NULL ) {
    fprintf(stderr, "couldn't open %s for reading\n", filename); 
    exit(1);
  }

  // obtain file size:
  fseek(fp, 0, SEEK_END);
  long lSize = ftell(fp);
  rewind(fp);

  // allocate memory to contain the whole file:
  char *buffer = malloc(lSize + 1);

  // copy the file into the buffer:
  size_t result = fread(buffer, 1, lSize, fp);
  if ( result != lSize ) {
    fprintf(stderr, "*** error reading from %s\n", filename);
    exit (1);
  }

  fclose (fp);

  *(buffer + lSize) = '\0';

  return buffer;
  
}
//----------------------------------------------------------------------------



int main() {

  char *sourcefilename = "mflib.lua";
  char *outfilename = "mflib_lua.c";
  
  char *libstr = string_from_file(sourcefilename);

  char *cursor = NULL;
  int advance = 0;

  //// escape quotes //////////////////////////////////////////////////////////
  while ( (cursor = strchr(libstr + advance, '"')) ){

    advance = cursor - libstr + 2; // how much to advance next search

    // make new string 1 char longer to accomodate '\\'
    int libstrbufflen = strlen(libstr) + 1;
    char *new = calloc(libstrbufflen + 1, 1); // make room for '\\'

    // copy chars up to cursor
    char *libstr_cursor = libstr;
    char *new_cursor = new;
    while ( libstr_cursor < cursor ) {
      *new_cursor++ = *libstr_cursor++;
    }

    *new_cursor++ = '\\';
    *new_cursor++ = '"';

    libstr_cursor++; // skip over found '"'

    // finish string
    while ( *libstr_cursor != '\0' ) {
      *new_cursor++ = *libstr_cursor++;
    }

    free(libstr); // free old string
    libstr = new; 

  }
  /////////////////////////////////////////////////////////////////////////////


  //// escape quotes //////////////////////////////////////////////////////////
  advance = 0;
  while ( (cursor = strchr(libstr + advance, '\n')) ){

    advance = cursor - libstr + 2; // how much to advance next search

    // make new string 1 char longer to accomodate '\\'
    int libstrbufflen = strlen(libstr) + 1;
    char *new = calloc(libstrbufflen + 1, 1); // make room for '\\'

    // copy chars up to cursor
    char *libstr_cursor = libstr;
    char *new_cursor = new;
    while ( libstr_cursor < cursor ) {
      *new_cursor++ = *libstr_cursor++;
    }

    *new_cursor++ = '\\';
    *new_cursor++ = 'n';

    libstr_cursor++; // skip over found '"'

    // finish string
    while ( *libstr_cursor != '\0' ) {
      *new_cursor++ = *libstr_cursor++;
    }

    free(libstr); // free old string
    libstr = new; 

  }
  /////////////////////////////////////////////////////////////////////////////



  printf("generating file %s using source %s\n", outfilename, sourcefilename);


  // wrap string in source file //////////////////////////////////////////////
  FILE * pFile;
  pFile = fopen (outfilename, "w");
  if ( pFile != NULL ) {
    fputs("// Generated by mflib_lua_to_source, DON'T EDIT\n\n", pFile);
    fputs("char *mflib_lua = \"", pFile);
    fputs(libstr, pFile);
    fputs("\";\n\n", pFile);
    fclose (pFile);
  }
  ////////////////////////////////////////////////////////////////////////////

  return(0);

}



